package com.ardf.controller;

import com.ardf.common.EntityConstants;
import com.ardf.entity.Group;
import com.ardf.entity.dto.GroupIdDto;
import com.ardf.service.IGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/group")
public class GroupController {
    private final IGroupService groupService;

    public GroupController(IGroupService groupService) {
        this.groupService = groupService;
    }

    /**
     * 根据id获取组别
     */
    @GetMapping("/get")
    public ResponseEntity<List<Group>> get(@RequestParam("competitionId") Long competitionId) {
        List<Group> list = groupService.list(new LambdaQueryWrapper<Group>()
                .eq(Group::getCompetitionId, competitionId)
                .orderByAsc(Group::getName));
        return ResponseEntity.ok(list);
    }

    /**
     * 根据年龄获取组别id
     */
    @GetMapping("/getByAge")
    public ResponseEntity<GroupIdDto> getByAge(@RequestParam("age") int age,
                                               @RequestParam("gender") String gender,
                                               @RequestParam("competitionId") Long competitionId) {
        GroupIdDto groupIdDto=groupService.getIdByAge(age,gender,competitionId);
        return ResponseEntity.ok(groupIdDto);
    }
    /**
     * 根据年龄获取组别id
     */
    @GetMapping("/getGroupByAge")
    public ResponseEntity<Group> getGroupByAge(@RequestParam("age") int age,
                                               @RequestParam("gender") String gender,
                                               @RequestParam("competitionId") Long competitionId) {
        Group group=groupService.getGroupByAge(age,gender,competitionId);
        return ResponseEntity.ok(group);
    }

    /**
     * 根据组别名获取组别id
     */
    @GetMapping("/getByName")
    public ResponseEntity<GroupIdDto> getByName(@RequestParam("groupName") String groupName,
                                                @RequestParam("gender") String gender,
                                                @RequestParam("competitionId") Long competitionId) {
        GroupIdDto groupIdDto = new GroupIdDto();
        Group group=null;
        if("公开组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }else if("小学低年级组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }else if("小学高年级组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }else if("初中组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }else if("高中组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }else if("大学组".equals(groupName)){
            group = groupService.getOne(new LambdaQueryWrapper<Group>()
                    .eq(Group::getName, groupName)
                    .eq(Group::getCompetitionId, competitionId));
        }
        else {
            if(EntityConstants.ATHLETE_GENDER_FEMALE.equals(gender)){
                group = groupService.getOne(new LambdaQueryWrapper<Group>()
                        .eq(Group::getName, "DXW")
                        .eq(Group::getCompetitionId, competitionId));
            }else {
                group = groupService.getOne(new LambdaQueryWrapper<Group>()
                        .eq(Group::getName, "DXM")
                        .eq(Group::getCompetitionId, competitionId));
            }
        }
        if(group==null){
            groupIdDto.setGroupId(null);
        }else {
            groupIdDto.setGroupId(group.getId());
        }
        return ResponseEntity.ok(groupIdDto);
    }

    /**
     * 批量增加
     */
    @PostMapping("/addBatch")
    public void addBatch(@RequestBody List<Group> groups) {
        groupService.saveAll(groups);
    }

    /**
     * 分页
     *
     * @param pageSize 每页多少条
     * @param pageNum  第几页
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<Page<Group>> page(@RequestParam("pageSize") Integer pageSize,
                                            @RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("competitionId") Long competitionId) {
        Page<Group> page = groupService.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Group>()
                .eq(Group::getCompetitionId, competitionId)
                .orderByAsc(Group::getName));
        return ResponseEntity.ok(page);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public void deleteBatch(@RequestBody List<Long> ids) {
        groupService.deleteAll(ids);
    }

    /**
     * 单个删除
     */
    @PostMapping("/deleteById")
    public void deleteById(@RequestParam("id") Long id) {
        groupService.deleteOne(id);
    }

}
